package com.link.base.base.crowd.controller;

import com.link.base.base.crowd.model.Crowd;
import com.link.base.base.crowd.model.CrowdAccntDet;
import com.link.base.base.crowd.service.CrowdAccntDetService;
import com.link.base.base.crowd.service.CrowdService;
import com.link.base.core.basic.util.CodeCleanUtils;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.controller.BasicControllerException;
import com.link.core.basic.service.BasicService;
import com.link.core.util.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 人群消费者明细表-
 *
 * @author 张宇浩
 * @date 2019/06/01 15:30
 */
@Controller
@RequestMapping("/link/crowdAccntDet")
public class CrowdAccntDetController extends BasicController<CrowdAccntDet> {

    @Resource
    private CrowdAccntDetService crowdAccntDetService;
    @Resource
    private CrowdService crowdService;

    @Override
    public BasicService<CrowdAccntDet> getBasicService() throws Exception {
        return crowdAccntDetService;
    }

    /**
     * 导出人群包openid和手机号
     *
     * @param entity {id,openidFlag,phoneFlag}
     */
    @RequestMapping(value = "/exportByCrowd")
    @ResponseBody
    public Map<String, Object> exportByCrowd(@RequestBody Crowd entity) {
        Map<String, Object> result = CodeCleanUtils.cast(new HashMap(16));
        // 允许导出标记
        final String exportFlagYes = "Y";
        // 不允许导出标记
        final String exportFlagNo = "N";
        Long crowdId = entity.getId();

        String openidFlag = entity.getOpenidFlag();
        if (StringUtils.isNull(openidFlag)) {
            openidFlag = exportFlagYes;
        }

        String phoneFlag = entity.getPhoneFlag();
        if (StringUtils.isNull(phoneFlag)) {
            phoneFlag = exportFlagNo;
        }
        try {
            if (null == crowdId) {
                throw new BasicControllerException("crowdId不能为空");
            }
            if (!StringUtils.equals(openidFlag, exportFlagYes) && !StringUtils.equals(phoneFlag, exportFlagYes)) {
                throw new BasicControllerException("openidFlag和phoneFlag两者至少要选中一个以上");
            }
            Crowd crowd = crowdService.queryById(crowdId);
            if (crowd == null) {
                throw new BasicControllerException("找不到id为[" + "]的人群包，请检查数据");
            }
            if (StringUtils.equals(openidFlag, exportFlagYes) && !StringUtils.equals(crowd.getOpenidFlag(), exportFlagYes)) {
                throw new BasicControllerException("没有权限导出openid，请检查人群包信息");
            }
            if (StringUtils.equals(phoneFlag, exportFlagYes) && !StringUtils.equals(crowd.getPhoneFlag(), exportFlagYes)) {
                throw new BasicControllerException("没有权限导出手机号，请检查人群包信息");
            }
            LCLogger.info().withMessageKey("controller.exportByCrowd")
                    .withMessage("crowdId:" + crowdId + ";openidFlag:" + openidFlag + ";phoneFlag:" + phoneFlag).flush();
            List<String> urls = crowdAccntDetService.exportByCrowd(crowdId, openidFlag, phoneFlag);
            result.put("urls", urls);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }
}
